package pl.com.qapps.datastore.berkeley;

import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

import pl.com.qapps.datastore.berkeley.impl.EntityBindings;

import com.google.appengine.api.datastore.Entity;
import com.google.appengine.api.datastore.EntityTranslator;
import com.google.appengine.api.datastore.Key;
import com.sleepycat.je.Database;
import com.sleepycat.je.DatabaseEntry;
import com.sleepycat.je.OperationStatus;
import com.sleepycat.je.Transaction;

@Deprecated
public class PutRequestDep extends BaseRequest<List<Key>> {

	private Iterable<Entity> entities;
	private final Map<DatabaseEntry, DatabaseEntry> entitiesData = new HashMap<>();

	void setEntities(Iterable<Entity> entities) {
		if (entities == null)
			throw new IllegalArgumentException("entities cannot be null");
		this.entities = entities;
		entitiesData.clear();
	}

	@Override
	List<Key> run(Store store, Transaction txn) {
		List<Key> keys = new LinkedList<>();
		Database db = store.getEntityDb();
		for (Entry<DatabaseEntry, DatabaseEntry> put : entitiesData.entrySet()) {
			OperationStatus result = db.put(txn, put.getKey(), put.getValue());
			if (result == OperationStatus.SUCCESS) {
				keys.add(EntityBindings.ENTITY_KEY_BINDING
						.entryToObject(put.getKey()));
			}
		}
		return keys;
	}

	@Override
	void init(Store store) {
		// TODO figure out why it was here
		throw new UnsupportedOperationException("not supported");
		// entitiesData.clear();
		// for (Entity entity : entities) {
		// EntityProto proto = EntityTranslator.convertToPb(entity);
		// DatabaseEntry keyData = new DatabaseEntry();
		// EntityBindings.ENTITY_BINDING.objectToKey(proto, keyData);
		// DatabaseEntry entityData = new DatabaseEntry();
		// store.getEntityValueBinding().objectToData(entity, entityData);
		// entitiesData.put(keyData, entityData);
		// }
	}
}
